# coding=utf-8
import time
import requests
import hashlib


INK_APPKEY = '3ab835466ca1716f2a7d597630495feb'
INK_APPID = 10006
INK_QR_URL = 'http://127.0.0.1:8000/inkpromo/qrcode/'
INK_QR_URL = 'http://qr.t.sohu.com/inkpromo/qrcode/'


SNS_APPID = 10003
SNS_APPKEY = '9068f336d85ed1c1c29d70136c125a69'
SNS_QR_URL = 'http://qrcode-dev.sce.sohuno.com/qrcode/'
SNS_QR_URL = 'http://qr.t.sohu.com/qrcode/'
# SNS_QR_URL = 'http://127.0.0.1:8000/qrcode/'


def access_api(api_url,
               method, headers=None, data=None, printif=None, files=None):
    # 访问api

    if method == "GET":
        # r = requests.get(api_url, params=data, headers=headers,timeout=10)
        r = requests.get(api_url, params=data, headers=headers)
    elif method == "POST":
        r = requests.post(api_url, data=data, headers=headers, files=files)
    elif method == "PUT":
        r = requests.put(api_url, data=data, headers=headers, files=files)
    elif method == "DELETE":
        r = requests.delete(api_url, data=data, headers=headers)
    return r


def sig(data_dict, appkey, exclude_keys=['sig']):
    sig_string = ""
    for key in sorted(data_dict.keys()):
        if key not in exclude_keys:
            sig_string += key + "=" + str(data_dict[key])
    sig_str = sig_string + appkey
    sig_str = sig_str.encode('utf-8')
    sig_md5 = hashlib.md5()
    sig_md5.update(sig_str)
    return sig_md5.hexdigest()


def ink_qrcode(long_url):
    data = {
        'avatar_url': 'http://www.yunsonbai.top/images/logo.png',
        'url': long_url,
    }
    data['flyer'] = str(int(1000 * time.time()))
    data['appid'] = INK_APPID
    data_sig = sig(data, INK_APPKEY)
    data['sig'] = data_sig
    api_url = INK_QR_URL
    res = access_api(api_url, method='POST', data=data)
    print(res.json())


def get_qrcode(long_url):
    data = {
        # 'icon': 'http://direct-message.bjcnc.img.sohucs.com/awardimg/default-award.png',
        'long_url': long_url,
        'need_icon': 0,
        'valid_time': 100,
    }
    data['flyer'] = str(int(1000 * time.time()))
    data['appid'] = SNS_APPID
    data_sig = sig(data, SNS_APPKEY)

    # 分配给需要特殊处理的需求(支持超短连接)
    # '10011': '1716f2a66ca171dsad7d5976466ca1v9',
    # data['appid'] = '10011'
    # data_sig = sig(data, '1716f2a66ca171dsad7d5976466ca1v9')
    data['sig'] = data_sig
    api_url = SNS_QR_URL
    # api_url = 'http://qr.t.sohu.com/qrcode/'
    res = access_api(api_url, method='GET', data=data)
    print(res.json())


def ink_qrcode_scannum(domain, short):
    api_url = "{0}/inkpromo/qrcode/scannum/".format(domain)

    data = {
        'short': short,
    }
    data['flyer'] = str(int(1000 * time.time()))
    data['appid'] = INK_APPID
    data_sig = sig(data, INK_APPKEY)
    data['sig'] = data_sig
    res = access_api(api_url, method='POST', data=data)
    print(res.json())


if __name__ == '__main__':
    # long_url = 'http://www.yunsonbai.top'
    # long_url = ('http://3g.k.sohu.com/snsj/profile/MTQxMkF'
    #             'CMkZGRTNEREJDNTU0ODVDOTZCODc0MTVFNjVAcXEuc29odS5jb20-')
    # ink_qrcode(long_url)
    # INK_QR_URL = 'http://qr.t.sohu.com/inkpromo/qrcode/'
    SNS_QR_URL = 'http://127.0.0.1:8000/qrcode/'
    # SNS_QR_URL = 'http://qrcode-test.sce.sohuno.com/qrcode/'
    SNS_QR_URL = 'http://qrcode-dev.sce.sohuno.com/qrcode/'
    INK_QR_URL = 'http://qrcode-dev.sce.sohuno.com/inkpromo/qrcode/'
    # INK_QR_URL = 'http://127.0.0.1:8000/inkpromo/qrcode/'
    long_url = 'http://sns-answer.sce.sohuno.com/dsadas'
    long_url = 'http://yunsonbai.top/2017/11/25/redis-mysql/'

    domain = "http://qrcode-dev.sce.sohuno.com"

    # poster
    SNS_APPID = 10003
    SNS_APPKEY = '9068f336d85ed1c1c29d70136c125a69'

    # 国青
    # SNS_APPID = 10027
    # SNS_APPKEY = '76252465288d18e636a905b5e7ea2cfa'

    # 10天过期
    SNS_APPID = 10038
    SNS_APPKEY = '7fb6f021862c330311d3d9f75b471dce'

    # 1天过期
    SNS_APPID = 10041
    SNS_APPKEY = 'bc7f330311d3d6f0218629f75b471dce'

    # get_qrcode(long_url)
    # ink_qrcode(long_url)

    ink_qrcode_scannum(domain, "ink6IwdnA")
